﻿// Listen for Request form extension.
chrome.extension.onRequest.addListener(NeoLi_PreXsnj);

var com_lipengbo_apps_xsnj_opened = false;

function NeoLi_PreXsnj(request, sender, callback) {
    if (request != null && request.com_lipengbo_apps_xsnj != null) {
        localStorage["com_lipengbo_apps_xsnj"] = request.com_lipengbo_apps_xsnj;
        callback(true);
    } else {
        callback(false);
    }
}

function NeoLi_Xsnj(source) {
    if (typeof($) == "undefined" || typeof($.ui) == "undefined") {
        NeoLi_jQueryCheck();
    } else {
        NeoLi_doXsnj();
    }
}

function NeoLi_jQueryCheck() {
    chrome.extension.sendRequest({jQueryAdded:0}, NeoLi_Xsnj);
}

function NeoLi_doXsnj() {
    if (typeof($) == "undefined") return;
    var xsnj = localStorage["com_lipengbo_apps_xsnj"];
    if (xsnj == null) {
        var url = "http://h2w.iask.cn/hd.php?&psize=12800&url=" + document.location.href;
        $.ajax({
            type:"POST",
            url:url,
            data:null,
            dataType:"html",
            success:function (source) {
                localStorage["com_lipengbo_apps_xsnj"] = source;
                NeoLi_addSource(source);
                NeoLi_showDialog();
            }
        });
    } else {
        NeoLi_addSource(xsnj);
        NeoLi_showDialog();
    }
}

function NeoLi_addjQueryUICss() {
    var link = document.createElement("link");
    link.rel = "stylesheet";
    link.type = "text/css";
    link.href = chrome.extension.getURL("css/jquery-ui.css");
    var headobj = document.getElementsByTagName('head')[0];
    headobj.appendChild(link);
}

function NeoLi_addH2WCss() {
    var link = document.createElement("link");
    link.rel = "stylesheet";
    link.type = "text/css";
    link.href = chrome.extension.getURL("css/high.css");
    var headobj = document.getElementsByTagName('head')[0];
    headobj.appendChild(link);
}

function NeoLi_addCSS() {
    NeoLi_addjQueryUICss();
    NeoLi_addH2WCss();
}

function NeoLi_addSource(source) {
    if (typeof($) == "undefined") return;
    NeoLi_moveHead();
    NeoLi_moveBody();
    $('<div />').hide().html(source).appendTo(document.body);
    $(".change_model").remove();
    $(".btnarea").remove();
    $(".tips_warp").remove();
    $(".gotop").remove();
    $(".c_statement").remove();
	var imgurl = chrome.extension.getURL("img/zheng50.gif");
    $(".footer").html("<img width='25' height='25' src='" + imgurl + "' />大挪移正文提取Beta1.0 ©2012 Neo.Li@GrapeCity.com");
    NeoLi_addCSS();
    NeoLi_createDialog();
}

function NeoLi_createDialog() {
    do {
        $("#model").dialog({
            title:"大挪移正文提取--按ESC键退出",
            autoOpen:false,
            resizable:false,
            draggable:true,
            modal:true,
            show:"slide",
            //        hide:"slide",  //jquery ui has bug!!!
            width:$(window).width() - 100,
            height:$(window).height() - 100,
            zIndex:10000,
            close:function () {
                com_lipengbo_apps_xsnj_opened = false;
                NeoLi_showBody();
            },
            open:function () {
                com_lipengbo_apps_xsnj_opened = true;
            }
        });
    } while ($.ui.dialog == null)
}

function NeoLi_showDialog() {
    while (com_lipengbo_apps_xsnj_opened == false) {
        var source = $("#model")[0];
        if (source != null) {
            $(source).dialog("open");
            //$(source).show();
            //com_lipengbo_apps_xsnj_opened = true
        }
    }
}

function NeoLi_moveBody() {
    var bodySource = document.body.innerHTML;
    localStorage["com_lipengbo_apps_body"] = bodySource;
    document.body.innerHTML = "";
}

function NeoLi_moveHead() {
    var headSource = document.getElementsByTagName('head')[0].innerHTML;
    localStorage["com_lipengbo_apps_head"] = headSource;
    document.getElementsByTagName('head')[0].innerHTML = "";
}

function NeoLi_showBody() {
    document.getElementsByTagName('head')[0].innerHTML = localStorage["com_lipengbo_apps_head"];
    document.body.innerHTML = localStorage["com_lipengbo_apps_body"];
    NeoLi_showPageAction();
}

function NeoLi_showPageAction() {
    chrome.extension.sendRequest({showPageAction:0}, null);
}